﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using ClassLibrary;
using System.IO;
using System.Drawing;
using System.Web.Caching;
using System.Data.SqlClient;
using System.Data;

namespace Webservice
{
    [WebService(Namespace = "http://172.18.67.166:147/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    [System.Web.Script.Services.ScriptService]
    public class Communicator : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string Login(string Username, string Password)
        {
            try
            {
                WebUser wu = WebUserController.Authenticate(Username, Password);
                if (wu != null)
                {
                    LogWriter.Print("User: " + wu.UserId.ToString() + " - Succesfully logged in", LogLevel.LOG);
                    return wu.UserId.ToString();
                }
            }
            catch (Exception exc)
            {
                LogWriter.Print("Exception Communicator->Login() " + exc, LogLevel.EXCEPTION);
            }
            LogWriter.Print("Login failed", LogLevel.LOG);
            return null;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List<ImageData> GetAllImageData(string UserId)
        {
            List<ImageData> l = new List<ImageData>();
            foreach (ImageData id in ImageDataController.GetAllImageData(new Guid(UserId)))
            {
                //due to some weird bug we need to manually check the id if they are empty or only-zero valued
                //and if they are we set them to their appropriate values
                //the userid of the ImageData is the in-parameter UserId
                //and part of the ImagePath holds the ImageId so we just parse it to get the ImageId
                id.UserId = (string.IsNullOrEmpty(id.UserId) ? UserId : id.UserId);
                string path = string.Format(@"C:\inetpub\wwwroot\Omegapoint\data\UploadedImages\{0}\", id.UserId);
                id.ImageId = (string.IsNullOrEmpty(id.ImageId) ? id.ImagePath.Replace(path, "").ToLower().Replace(".png", "").Replace(".jpg", "").Replace(".jpeg", "") : id.ImageId);
                
                //only expose part of the URL for the images
                id.ImagePath = id.ImagePath.Replace("C:\\inetpub\\wwwroot\\Omegapoint\\", "");
                
                l.Add(id);
            }
            return l;
        }

        //Uppdatera en ImageData 
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void UpdateImageData(string ImageId, string PatientId, string BodyPart, string ReasonForVisit, string Diagnosis, string ConsentForResearch, string ConsentForEducation, string ConsentForStorage)
        {
            ImageDataController.UpdateImageData(ImageId, PatientId, BodyPart, ReasonForVisit, Diagnosis, ConsentForResearch, ConsentForEducation, ConsentForStorage);
        }
    }
}